package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.LayoutStyle.ComponentPlacement;

public class UpdateAdo extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField tel;
	private JTextField rep;
	private JTextField grant;
	private JTextField etcAddr;
	private JTextField zip1;
	private JTextField zip2;
	private JTextField addr;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			UpdateAdo dialog = new UpdateAdo();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public UpdateAdo(final String ano, final int r, final JTable table) {
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel label = new JLabel("회원이름");
		label.setFont(new Font("굴림", Font.BOLD, 15));
		name = new JTextField();
		name.setColumns(10);
		JLabel label_3 = new JLabel("전화번호");
		label_3.setFont(new Font("굴림", Font.BOLD, 15));
		tel = new JTextField();
		tel.setColumns(10);
		JLabel label_4 = new JLabel("담당직원");
		label_4.setFont(new Font("굴림", Font.BOLD, 15));
		rep = new JTextField();
		rep.setColumns(10);
		JLabel label_5 = new JLabel("지급금액");
		label_5.setFont(new Font("굴림", Font.BOLD, 15));
		grant = new JTextField();
		grant.setColumns(10);
		
		JLabel label_1 = new JLabel("나머지주소");
		label_1.setFont(new Font("굴림", Font.BOLD, 15));
		
		etcAddr = new JTextField();
		etcAddr.setColumns(10);
		
		JLabel label_6 = new JLabel("우편번호");
		label_6.setFont(new Font("굴림", Font.BOLD, 15));
		
		JLabel label_7 = new JLabel("기본주소");
		label_7.setFont(new Font("굴림", Font.BOLD, 15));
		
		zip1 = new JTextField();
		zip1.setColumns(4);
		
		JLabel label_8 = new JLabel("-");
		label_8.setFont(new Font("굴림", Font.BOLD, 12));
		
		zip2 = new JTextField();
		zip2.setColumns(4);
		
		JButton findBtn = new JButton("우편번호 찾기");
		findBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZipcodeSub z = new ZipcodeSub( zip1, zip2, addr, etcAddr );
				z.setVisible(true);
			}
		});
		
		addr = new JTextField();
		addr.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap(144, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(55)
							.addComponent(tel, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(55)
							.addComponent(rep, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(55)
							.addComponent(grant, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE))
						.addComponent(label_7, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
							.addGap(35)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(etcAddr, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE)
								.addComponent(addr, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(label_6, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
								.addComponent(label, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
							.addGap(50)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(name, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(zip1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(label_8, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(zip2, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(findBtn)))))
					.addGap(89))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap(45, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_6, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(3)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(zip1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_8)
								.addComponent(zip2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(findBtn))))
					.addGap(13)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_7, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(10)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addComponent(etcAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(3)
							.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
						.addComponent(tel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(3)
							.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
						.addComponent(rep, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(3)
							.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
						.addComponent(grant, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(82))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("수정완료");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						AdoptersVO a = new AdoptersVO(ano, name.getText(), addr.getText(), etcAddr.getText(), tel.getText(), null, rep.getText(), grant.getText());
						int cnt = new AdoptersDAO().updateAdopter(a);
						if(cnt > 0){
							JOptionPane.showMessageDialog(null, "수정완료!!");
							table.setValueAt(name.getText(), r, 1);
							table.setValueAt(tel.getText(), r, 2);
							table.repaint();
							dispose();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("수정취소");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		AdoptersVO a = new AdoptersDAO().selectOneAdopter(ano);
		
		String zipAA = a.getAddress().split(" ")[0].substring(0, 3);
		String zipBB = a.getAddress().split(" ")[0].substring(3, 6);
		String subAddr = a.getAddress().substring(7);
		
		name.setText(a.getAdoname());
		zip1.setText(zipAA);
		zip2.setText(zipBB);
		addr.setText(subAddr);
		etcAddr.setText(a.getAddress_spec());
		tel.setText(a.getTel());
		rep.setText(a.getRepno());
		grant.setText(a.getGrants());
	}

}
